$(document).ready(function () {
        let version = {};
        let agreementList = {};
        const path = getRequsetPath();
        const getLatestVersion = async () => {
            $.ajax({
                url: path + '/api/home/app_latest_version',
                type: 'POST',
                dataType: 'json',
                success: function (response) {
                    version = response.data.latest_version;
                },
                error: function (xhr, status, error) {
                    console.log('请求失败:', error);
                },
            });
        };

        const goDiscover = () => {
            const env = getEnv();
            const redirectUrl = env === "production" ? "https://web.goupapp.com/app/" : "https://web.ntzq.tech/app/";
            window.open(redirectUrl + "login");
        };

        const downloadApp = () => {
            if (!version.version) return;
            const staticUrl = getStaticUrl();
            window.open(staticUrl + `/app/Goup_Setup_v${version.version}.exe`);
        };

        $("img.item-background").on("load", function () {
            $("#goup-homepage").show();
        });

        $(".download-text").click(downloadApp);
        $(".web-text").click(goDiscover);
        $(".download-button").click(downloadApp);
        $(".web-button").click(goDiscover);
        getLatestVersion();
        let page = {
            page: 1,
            size: 5
        }


        const getNewsList = () => {
            $.ajax({
                url: path + '/api/news/news_list',
                type: 'POST',
                data: {
                    page: page.page,
                    size: page.size
                },
                dataType: 'json',
                success: function (response) {
                    page = response.data;
                    updateNewsList();
                    updatePagination();
                },
                error: function (xhr, status, error) {
                    console.log('请求失败:', error);
                },
            });
        }
        const updateNewsList = () => {
            const newsListContainer = $('.news-list');
            let newsItemHtml = "";
            newsListContainer.empty();
            page.content.forEach(item => {
                newsItemHtml = `
                    <div class="news-item" data-id="${item.id}">
                        <img alt="" class="item-cover" src="${item.cover}">
                        <div class="item-details">
                            <div class="item-title">
                                <span class="item-title-text">
                                    ${item.title}
                                </span>
                                <span class="item-title-time">
                                    ${item.created_at}
                                </span>
                            </div>
                            <div class="item-subtitle">
                                ${item.summary}
                            </div>
                            <div class="read-count">
                                 <span class="news-read-count">
                                    <img alt="" src="../../assets/svg/eye.svg" class="eye-icon">
                                   ${item.count}
                                </span>
                            </div>
                        </div>
                    </div>
                `;
                newsListContainer.append(newsItemHtml);
            })
            $('.news-item').on('click', function () {
                const id = $(this).data('id');
                const item = page.content.find(news => news.id == id);
                window.open(item.url);
                $.ajax({
                    url: path + '/api/news/news_count',
                    type: 'POST',
                    data: {
                        id:id
                    },
                    dataType: 'json',
                    success: function (response) {
                        version = response.data.latest_version;
                    },
                    error: function (xhr, status, error) {
                        console.log('请求失败:', error);
                    },
                });
            });
        };


        const updatePagination = () => {
            $('.pagination-left').html(`共${page.totalElements}条资讯  第${page.page}/${page.totalPages}页`);
            const paginationContainer = $('.pagination-content');
            paginationContainer.empty();

            if (page.totalPages <= 5) {
                // 如果总页数小于等于5页，直接显示所有页码
                for (let i = 0; i < page.totalPages; i++) {
                    let paginationItemClass = 'pagination-index';  // 默认类名
                    if (i + 1 === page.page) {
                        paginationItemClass += ' pagination-active';  // 当前页加上active类
                    }
                    const paginationItem = `<span class="${paginationItemClass}" data-page="${i + 1}">${i + 1}</span>`;
                    paginationContainer.append(paginationItem);
                }
            } else {
                let index = 0;
                for (let i = 1; i <= page.totalPages; i++) {
                    let paginationItemClass = 'pagination-index';  // 默认类名

                    if (i == page.page) {
                        paginationItemClass += ' pagination-active';  // 当前页加上active类
                    }

                    if (i == 1 || i == page.page || i == page.totalPages || i == page.totalPages - 1 || (i == 2 && (page.page == 1 || page.page > page.totalPages - 2))) {
                        const paginationItem = `<span class="${paginationItemClass}" data-page="${i}">${i}</span>`;
                        paginationContainer.append(paginationItem);
                        if (index === 1) {
                            paginationContainer.append(`<span>...</span>`);
                        }
                        index++;
                    }
                }
                $('.pagination-index').on('click', function () {
                    const page_data = $(this).data('page');
                    if (page.page == page_data) return;
                    page.page = page_data;
                    getNewsList();
                });

            }


        }
        // getNewsList();
        $('.pagination-prod').on('click', function () {
            if (page.page == 1) return;
            page.page--;
            getNewsList();
        });
        $(document).on('click', '.pagination-next', function () {
            if (page.page == page.totalPages) return;
            page.page++;
            getNewsList();
        });
        $(".menu-item-homepage").click(function () {
            window.location.href = "../../index.html";
        });
    }
)
